package at.ac.uniklu.rallyemaster.activities;

import java.text.NumberFormat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import at.ac.uniklu.rallyemaster.R;
import at.ac.uniklu.rallyemaster.RallyeMaster;
import at.ac.uniklu.rallyemaster.adapters.DBTrackingPoint;
import at.ac.uniklu.rallyemaster.domain.TrackingPoint;

public class InformationActivity extends Activity{
	
	static final private int EDIT_ITEM = Menu.FIRST; 
	static final private int DELETE_ITEM = Menu.FIRST +1;
	static final private int OPTIONS_ITEM = Menu.FIRST +2;
	
	private DBTrackingPoint dbTrackingPoint;
	private TrackingPoint trackingPoint;
	
	//inits the fields for the informations
	private EditText location;
	private EditText avgSpeed;
	private EditText speed;
	private EditText distance;
	private EditText notes;
	
	// photo and video views
	//TODO insert
	
	private CheckBox isCurve;
	private RadioGroup curveDirection;
	private TextView directionView;
	private RadioButton leftCurve;
	private RadioButton rightCurve;
	private RatingBar ratingCurve;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.information_layout);
		//Getting selected Point ID
		int pointID = getIntent().getExtras().getInt("PointID",-1);
		
		//getting point information from db
		dbTrackingPoint = new DBTrackingPoint(); 
		trackingPoint = dbTrackingPoint.getTrackByID(pointID);
		RallyeMaster.logDebug("Found Tracking Point with id: " + trackingPoint.getId());
		
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMaximumFractionDigits(4);
		
		location = (EditText) findViewById(R.id.location);
		location.setText(nf.format(trackingPoint.getLatitude()) + " / " + nf.format(trackingPoint.getLongitude()));
		
		avgSpeed = (EditText) findViewById(R.id.avgSpeed);
		avgSpeed.setText((int)trackingPoint.getAvgSpeed() + " km/h");
		
		speed = (EditText) findViewById(R.id.speed);
		speed.setText((int)trackingPoint.getCurrentSpeed() + " km/h");
		
		distance = (EditText) findViewById(R.id.distance);
		distance.setText(nf.format(trackingPoint.getDistance()) + " m");
		
		notes = (EditText) findViewById(R.id.note);
		notes.setText(trackingPoint.getNotes());
		
		isCurve = (CheckBox)findViewById(R.id.checkCurve);
		isCurve.setChecked(trackingPoint.getCurve());
		
		
		ratingCurve =(RatingBar)findViewById(R.id.ratingcurve);
		directionView = (TextView)findViewById(R.id.direction_view);
		
		if(isCurve.isChecked()){
			if(trackingPoint.getCurveDirection() == TrackingPoint.CURVE_LEFT){
				leftCurve= (RadioButton) findViewById(R.id.left);
				leftCurve.setEnabled(true);
			}else{
				rightCurve= (RadioButton) findViewById(R.id.right);
				rightCurve.setEnabled(true);
			}
			ratingCurve.setRating(trackingPoint.getCurveIntension());
		}else{
			isCurve.setVisibility(CheckBox.GONE);
			directionView.setVisibility(TextView.GONE);
			curveDirection = (RadioGroup) findViewById(R.id.curveDirection);
			curveDirection.setVisibility(RadioGroup.GONE);
			ratingCurve.setVisibility(RatingBar.GONE);
		}
	}

	@Override 
	public boolean onCreateOptionsMenu(Menu menu){
		MenuItem editItem = menu.add(0, EDIT_ITEM, Menu.NONE, R.string.itemMenu_edit);
		MenuItem deleteItem = menu.add(0,DELETE_ITEM, Menu.NONE, R.string.itemMenu_delete);
		MenuItem optionsItem = menu.add(0, OPTIONS_ITEM, Menu.NONE, R.string.itemMenu_options);
		
		editItem.setIcon(android.R.drawable.ic_menu_edit);
		deleteItem.setIcon(android.R.drawable.ic_menu_delete);
		optionsItem.setIcon(android.R.drawable.ic_menu_preferences);
		
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onContextItemSelected(item);
		switch(item.getItemId()){
		case EDIT_ITEM:{
			Intent intent = new Intent(this, ChangeInformationActivity.class);
			intent.putExtra("PointID", trackingPoint.getId());
			startActivity(intent);
			this.finish();
			return true;
		}
		case DELETE_ITEM:{
			dbTrackingPoint.removeTrackingPoint(trackingPoint.getId());
			RallyeMaster.logDebug("Trying to delete tp: "+ trackingPoint.getId());
			Toast toast = Toast.makeText(getApplicationContext(), R.string.notification_delete, Toast.LENGTH_SHORT);
			toast.show();
			this.finish();
			return true;
		}
		case OPTIONS_ITEM:{
			Intent intent = new Intent(this, PreferencesActivity.class);
			startActivity(intent);
			return true;
		}
		}
		return false;
	}

	
}
